import { computed, watch, unref } from 'vue';
import { useRouter } from 'vue-router';

export function useModelWrapper(props, emit, name = 'modelValue') {
  return computed({
    get: () => props[name],
    set: (value) => emit(`update:${name}`, value)
  });
}

export function useSetPageTitle(pageTitle) {
  const { currentRoute } = useRouter();
  watch(
    () => currentRoute.value.path,
    () => {
      const route = unref(currentRoute);

      const title = pageTitle || route?.meta?.title;
      document.title = title || '宣教助手';
    },
    {
      immediate: true,
      deep: true,
      flush: 'post'
    }
  );

  const setPageTitle = (title) => {
    document.title = title;
  };
  return { setPageTitle };
}
/**
 * 获取分页数据的加载状态
 */
export const usePageLoadStatus = (pageBean) => {
  const loadStatus = computed(() => {
    return {
      empty: pageBean.data.total === 0,
      loading: pageBean.data.records.length === 0 && pageBean.data.total !== 0,
      allLoaded: pageBean.data.records.length === pageBean.data.total
    };
  });

  return loadStatus;
};
